package com.yunhe.tianhe.rentcars.service.returnCars.Impl;/**
 * Created by Administrator on 2017/12/4 0004.
 */

import com.yunhe.tianhe.rentcars.dao.returnCars.ReturnCarsDao;
import com.yunhe.tianhe.rentcars.entity.cheliangguanli.XinXi;
import com.yunhe.tianhe.rentcars.entity.cheliangguanli.ZuYong;
import com.yunhe.tianhe.rentcars.entity.system.Dept;
import com.yunhe.tianhe.rentcars.service.cheliangguanli.XinXiService;
import com.yunhe.tianhe.rentcars.service.returnCars.ReturnCarsService;
import com.yunhe.tianhe.rentcars.service.system.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @user yanxiu
 * @creat 2017-12-04 下午 5:36
 * @desc
 **/
@Service
public class ReturnCarServiceImpl implements ReturnCarsService {

    @Autowired
    private ReturnCarsDao returnCarsDao;
    @Autowired
    private XinXiService xinXiService;
    @Autowired
    private DeptService deptService;

    @Override
    public ZuYong findOrder(long orderId) {
        ZuYong order = returnCarsDao.findOrder(orderId);
        if(null!=order) {
            Date readyetTime = order.getReadetTime();
            Date readybtTime = order.getReadybtTime();
            if (null != readyetTime & null != readybtTime) {   //租期=预计还车时间-预计取车时间
                String rentDays = ReturnCarServiceImpl.getRentDays(readyetTime, readybtTime);
                order.setRentDays(rentDays);
            }
            long carId = order.getCarId();      //获取汽车信息
            XinXi car = xinXiService.find(carId);
            order.setIsAutoStr(car.getIsAuto());  //手动、自动
            order.setCarBrand(car.getCarBrand()); //品牌
            order.setCarColor(car.getCarColor());  //颜色
            order.setCarBrandType(car.getCarBrandType());//品牌种类
            order.setEngineSize(car.getEngineSize()); //排量
            order.setSeatNum(car.getSeatNum()); //座位数


            long btrentDept = order.getBtrentDept();  //获取门店信息
            Dept dept = deptService.findOne(btrentDept);
            order.setInCity(dept.getInCity()); //城市
            order.setDeptName(dept.getDeptName()); //店名
            order.setAddress(dept.getAddress()); //详细地址
        }
        return order;
    }

    //计算时间差
    public static String getRentDays(Date d1,Date d2){
        long diff = d1.getTime() - d2.getTime();
        long days = diff / (1000 * 60 * 60 * 24);
        String rentDaysStr=days+"天";
        return rentDaysStr;
    }
}
